package com.app.service.impl;

import com.app.common.FinancingTool;
import com.app.common.ScaleTool;
import com.app.mapper.ComRecruitersMapper;
import com.app.mapper.EnterpriseMapper;
import com.app.model.*;
import com.app.service.ComRecruitersService;
import com.app.service.EnterpriseService;
import com.app.service.JobPublishService;
import com.app.util.StringTool;
import com.app.vo.CustomerListVo;
import com.app.vo.EnterpriseVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.List;


/**
 * @Description:公司实现类
 * @Author: N
 * @Date: 2022年11月24日 10点29分
 */
@Service
public class EnterpriseServiceImpl implements EnterpriseService {

    @Autowired
    EnterpriseMapper enterpriseMapper;
    @Autowired
    ComRecruitersService comRecruitersService;
    @Autowired
    JobPublishService jobPublishService;

    @Override
    public int insert(EnterpriseModel model) {
        return enterpriseMapper.insert(model);
    }

    @Override
    public EnterpriseModel findModelById(int id) {
        QueryWrapper<EnterpriseModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select().eq("id", id);
        List<EnterpriseModel> userList = enterpriseMapper.selectList(queryWrapper);
        if (userList.isEmpty()) {
            return null;
        } else {
            return userList.get(0);
        }
    }

    @Override
    public void update(EnterpriseModel model) {
        enterpriseMapper.updateById(model);
    }

    @Override
    public List<EnterpriseModel> findAllByName(String comName) {
        QueryWrapper<EnterpriseModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id", "enterprise_name");
        if (StringTool.isNotEmpty(comName)) {
            queryWrapper.like("enterprise_name", comName);
        }

        List<EnterpriseModel> userList = enterpriseMapper.selectList(queryWrapper);
        if (userList.isEmpty()) {
            return null;
        } else {
            return userList;
        }
    }

    @Override
    public Page<EnterpriseModel> findAllByNamePage(String comName, Integer page, Integer limit) {
        QueryWrapper<EnterpriseModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id", "enterprise_name", "erterprise_id_num", "street_id");
        if (StringTool.isNotEmpty(comName)) {
            queryWrapper.like("enterprise_name", comName);
        }
        Page<EnterpriseModel> pages = new Page<>();
        pages.setCurrent(page);
        pages.setSize(limit);
        Page<EnterpriseModel> EnterpriseModel = enterpriseMapper.selectPage(pages, queryWrapper);
        if (EnterpriseModel == null) {
            return null;
        } else {
            return EnterpriseModel;
        }
    }

    @Override
    public List<EnterpriseModel> findModelByName(String comName) {
        QueryWrapper<EnterpriseModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id", "enterprise_name", "logo_url").like("enterprise_name", comName);
        List<EnterpriseModel> userList = enterpriseMapper.selectList(queryWrapper);
        if (userList.isEmpty()) {
            return null;
        } else {
            return userList;
        }
    }

    @Override
    public List<EnterpriseModel> findModelAll(EnterpriseModel model) {
       List<EnterpriseModel> list = enterpriseMapper.getComPopular(model);
       return list;
    }

    @Override
    public EnterpriseModel findLogoById(int id) {
        QueryWrapper<EnterpriseModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id", "enterprise_name", "logo_url").eq("id", id);
        List<EnterpriseModel> modelList = enterpriseMapper.selectList(queryWrapper);
        if (modelList == null) {
            return null;
        } else {
            return modelList.get(0);
        }
    }

    @Override
    public Page<EnterpriseModel> getModelList(EnterpriseModel model) {
        QueryWrapper<EnterpriseModel> queryWrapper = new QueryWrapper<>();

        if (model.getScaleId() != null && model.getScaleId() > 0) {
            queryWrapper.eq("scale_id", model.getScaleId());
        }
        if (model.getState() != null && model.getState() > 0) {
            queryWrapper.eq("state", model.getState());
        }
        if (StringTool.isNotEmpty(model.getKeyWord())) {
            queryWrapper.and(wrapper -> wrapper.like("enterprise_name", model.getKeyWord()).or().like("legal_person_name", model.getKeyWord()));
        }
        queryWrapper.orderByDesc("crt_time");
        Page<EnterpriseModel> page = new Page<>();
        page.setCurrent(model.getPage());
        page.setSize(model.getLimit());
        Page<EnterpriseModel> EnterpriseModel = enterpriseMapper.selectPage(page, queryWrapper);
        if (EnterpriseModel == null) {
            return null;
        } else {
            return EnterpriseModel;
        }
    }

    @Override
    public int getModelCount(EnterpriseModel model) {
        QueryWrapper<EnterpriseModel> queryWrapper = new QueryWrapper<>();
        if (model != null && model.getState() != null) {
            queryWrapper.eq("state", model.getState());
        }
        int count = enterpriseMapper.selectCount(queryWrapper);
        return count;
    }

    @Override
    public EnterpriseVo getCompanyList(EnterpriseModel model) {
        Page<EnterpriseModel> modelPage = getModelList(model);
        List<EnterpriseModel> modelList = modelPage.getRecords();
        for (int i = 0; i < modelList.size(); i++) {
            modelList.get(i).setHrCount(comRecruitersService.getCountByComId(modelList.get(i).getId()));
            modelList.get(i).setJobPublishCount(jobPublishService.getJobPublishCountByComId(modelList.get(i).getId()));
            if (modelList.get(i).getScaleId() != null) {
                for (int j = 0; j < ScaleTool.SCALE_VO_LIST.size(); j++) {
                    if (modelList.get(i).getScaleId() == ScaleTool.SCALE_VO_LIST.get(j).getId()) {
                        modelList.get(i).setScaleName(ScaleTool.SCALE_VO_LIST.get(j).getData());
                    }

                }
            }

            for (int k = 0; k < FinancingTool.FINANCING_VO_LIST.size(); k++) {
                if (modelList.get(i).getFinancingId() != null && modelList.get(i).getFinancingId() == FinancingTool.FINANCING_VO_LIST.get(k).getId()) {
                    modelList.get(i).setFinancingName(FinancingTool.FINANCING_VO_LIST.get(k).getName());
                }
            }
        }
        int count = (int) modelPage.getTotal();
        EnterpriseVo vo = new EnterpriseVo();
        vo.setCount(count);
        vo.setList(modelList);
        return vo;
    }

    @Override
    public List<EnterpriseModel> findModelByIds(String[] comIds) {
        QueryWrapper<EnterpriseModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id", "enterprise_name", "logo_url").in("id", comIds);
        return enterpriseMapper.selectList(queryWrapper);
    }

    @Override
    public Integer selectEmployersCount() {
        return enterpriseMapper.selectCount(null);
    }

    @Override
    public List<EnterpriseModel> selectAll() {
        return enterpriseMapper.selectList(null);
    }

    @Override
    public Page<EnterpriseModel> employersList(Integer page1, Integer limit) {
        QueryWrapper<EnterpriseModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("crt_time");
        Page<EnterpriseModel> page = new Page<>();
        page.setCurrent(page1);
        page.setSize(limit);
        Page<EnterpriseModel> EnterpriseModel = enterpriseMapper.selectPage(page, queryWrapper);
        if (EnterpriseModel == null) {
            return null;
        } else {
            return EnterpriseModel;
        }
    }

    @Override
    public List<EnterpriseModel> enterprise(Integer page, Integer limit, Integer streetId) {

        return null;
    }

    @Override
    public List<EnterpriseModel> getComByStreet(String startTime, String endTime) {
        return enterpriseMapper.getComByStreet(startTime, endTime);
    }

    @Override
    public List<EnterpriseModel> getComAndStreet(String startTime, String endTime) {
        List<EnterpriseModel> list = enterpriseMapper.getComAndStreet(startTime, endTime);
        return list;
    }

    @Override
    public EnterpriseVo getComPageByStreetId(EnterpriseModel model) {

        QueryWrapper<EnterpriseModel> queryWrapper = new QueryWrapper<>();
        if(model.getStreetId()!=null&&model.getStreetId()>0)
        {
            queryWrapper.eq("street_id", model.getStreetId());
        }
        if(StringTool.isNotEmpty(model.getStartTime()))
        {
            queryWrapper.apply(" TO_DAYS(crt_time) >= TO_DAYS('"+model.getStartTime()+"')");
        }
        if(StringTool.isNotEmpty(model.getEndTime()))
        {
            queryWrapper.apply(" TO_DAYS(crt_time) <= TO_DAYS('"+model.getEndTime()+"')");
        }
        Page<EnterpriseModel> pages = new Page<>();
        pages.setCurrent(model.getPage());
        pages.setSize(model.getLimit());
        Page<EnterpriseModel> page = enterpriseMapper.selectPage(pages, queryWrapper);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (page == null) {
            return null;
        } else {
            EnterpriseVo vo = new EnterpriseVo();
            vo.setList(page.getRecords());
            vo.setCount((int) page.getTotal());
            return vo;
        }
    }
    @Override
    public Page<EnterpriseModel> getKeynotePage(Integer page, Integer limit) {
        QueryWrapper<EnterpriseModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("enterprise_name","甘肃普罗生物科技有限公司","玉门市求鹏能源有限公司","甘肃美润新材料科技有限公司","甘肃骏轩医药化工有限公司","玉门朗天生物科技有限公司","甘肃盛诺医药科技有限公司","甘肃泰尔精细化工有限公司","甘肃喆淇化工有限公司","酒泉市尚上化工有限公司","玉门鑫时利气体有限公司","玉门市海鼎化工有限公司","酒泉宇鹏化工科技有限公司","玉门明华化学有限公司","玉门市一家生物科技有限公司","玉门大洋天庆石化有限责任公司","甘肃鲁玉能源科技有限公司","玉门市华事达能源有限公司","甘肃凯盛大明光能科技有限公司","玉门市艾郎风电科技发展有限公司","甘肃盈华环保科技有限公司","甘肃省西部华颀材料科技有限公司","玉门优顺化工有限公司","甘肃汉隆化工有限公司","玉门市中意面业有限责任公司","玉门大业草业科技发展有限责任公司","甘肃天祥农业科技开发有限公司","玉门敬酒食品工业有限公司","甘肃金利通碳材料科技有限公司","酒泉市浩海煤化有限公司","玉门千华制药有限公司","酒泉西部天成新材料有限公司","玉门峪福生物科技有限公司","甘肃世纪德坤化工有限公司","酒泉亚佳化学有限公司","甘肃金色润盛化工有限公司","玉门富凯新能源科技有限责任公司","甘肃金特化学有限公司","甘肃汇农丰生物科技有限公司","玉门市静洋钛白粉制造有限责任公司","甘肃省中胜浩顺科技有限公司","甘肃勤业化工有限公司","玉门祥天新能源产业发展有限公司","甘肃西部水泥有限责任公司","玉门市雄关天石水泥有限责任公司","甘肃农垦饮马水泥有限责任公司","玉门市荣鑫混凝土有限公司","玉门鸿腾商品混凝土有限公司","玉门市天鸿化工有限公司","玉门新节源泥浆材料有限公司","龙源（玉门）新能源发展有限公司","玉门市融心热能有限责任公司","惠记大地（玉门）新能源有限公司","甘肃西部鑫宇化学有限公司","玉门市陆丰矿业有限公司","玉门市祥泰矿业有限责任公司","玉门市昌源矿业有限公司","中国石油天然气股份有限公司玉门油田分公司","玉门市尚能科技有限有限公司","玉门市坤锦化工有限公司","酒泉紫峰化工新材料科技有限公司","玉门龙川高科技发展有限公司","玉门市通利水泥制品有限公司","玉门锦辉长城电力设备制造有限公司","甘肃洁源风电有限责任公司","中核甘肃风力发电有限公司","中海油新能源玉门风电有限公司","中节能港建（甘肃）风力发电有限公司","甘肃华电玉门风力发电有限公司","中核玉门黑崖子新能源有限公司","华能玉门风电有限责任公司","大唐玉门新能源有限公司","大唐玉门昌马风电有限公司","中节能（甘肃）风力发电有限公司","甘肃大唐玉门风电有限公司","甘肃中电桥湾风力发电有限公司","中核玉门七墩滩风电有限公司","甘肃玉门汇能新能源开发有限责任公司","中节能太阳能（酒泉）发电有限公司","玉门中利腾晖新能源电力有限公司","中科恒源（玉门）新能源科技有限公司","玉门市永联科技新能源有限公司","王门绿欣农业科技开发有限责任公司","富云新能源有限公司","玉门市良栋选煤有限公司","风渡新材料（玉门）有限公司","玉门首航光学有限公司","甘肃凯盛硅基新材料有限公司","玉门市得利混凝土","王门市科皓催化材料有限公司","甘肃宏海能源有限公司","甘肃中车风能科技有限公司","宝能（甘肃）新能源集团有限公司");
        queryWrapper.orderByDesc("crt_time");
        Page<EnterpriseModel> enterpriseModelPage = new Page<>();
        enterpriseModelPage.setCurrent(page);
        enterpriseModelPage.setSize(limit);
        Page<EnterpriseModel> EnterpriseModel = enterpriseMapper.selectPage(enterpriseModelPage, queryWrapper);
        if (EnterpriseModel == null) {
            return null;
        } else {
            return EnterpriseModel;
        }
    }

}
